const app = getApp()
import {
  imageUrl
} from "../../utils/imageutil.js"
import {
  to_tim_chat
} from "../../utils/to_tim_chat"
Page({
  data: {
    imageUrl,
    tanflag: false,
    scroll: '',
    
    new_scoll_h: '',
    num: getApp().globalData.unreadCount,
    list: [],
    _index: '',
    st: '',
    et: '',
    type: 0,
    money_st: '',
    money_et: '',
    search: '',

    nav: [
      // {
      //   title: '平台订单',
      //   type: 2
      // },
      {
        title: '订单中心',
        type: 3
      },
      {
        title: '平台消息',
        type: 1
      }
    ],
    nav_curr: 3,
    scroll_h: 100,
    is_user: true,
    is_merchant: false,
    is_aunt: false,
    OrderType: [{
        title: '全部',
        id: 0
      }, {
        title: '已接单',
        id: 6
      },
      {
        title: '待接单',
        id: 12
      },
      {
        title: '已取消',
        id: 13
      },
    ],
    date: '',
    newdate: '',
    array: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000,
      16000, 17000, 18000, 19000, 20000, 21000, 22000, 23000, 24000, 25000, 26000, 27000, 28000,
      29000, 30000
    ],
    new_array: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000,
      15000, 16000, 17000, 18000, 19000, 20000, 21000, 22000, 23000, 24000, 25000, 26000, 27000,
      28000, 29000, 30000
    ],
    new_index: '',

    HomeList: [{
        title: '全部',
        id: 1
      },
      {
        title: '住家',
        id: 2
      },
      {
        title: '不住家',
        id: 3
      }
    ],

    searchself_list: [{
        title: '否',
        id: 1
      },
      {
        title: '是',
        id: 2
      }
    ],

    keys: '',

    article_page: 1,
    article_list: [],
    article_flag: true,
    types: [{ //顶部分类
      title: '订单类型',
      id: '1'
    }, {
      title: '工作区域',
      id: '2'
    }, {
      title: '上工时间',
      id: '3'
    }],
    ay_order: [],
    typescurr: -1, //顶部tab切换状态
    list2_index: -1, //顶部class
    // 6=护工，5=育儿嫂，4=育婴嫂，2=保姆，1=月嫂

    typeCity: 1,
    province: '',

    city: '',
    region: '',
    street: '',
    province_num: 0,
    city_num: 0,
    region_num: 0,
    street_num: 0,
    value: [0, 0, 0, 0],
    range_list: [],
    province_list: [],
    city_list: [],
    region_list: [],
    street_list: [],
    area: "",
    work_type: [{
        id: '',
        name: "全部"
      },
      {
        id: 6,
        name: "护工"
      },
      {
        id: 5,
        name: "育儿嫂"
      },
      {
        id: 4,
        name: "育婴嫂"
      },
      {
        id: 2,
        name: "保姆"
      },
      {
        id: 1,
        name: "月嫂"
      },
    ],
    is_live_room: [{
        id: '',
        name: "全部"
      },
      {
        id: 0,
        name: "住家"
      },
      {
        id: 1,
        name: "白班"
      },

    ],
    order_type: "",
    order_time: "",
    state: 0,
    order_list: [],
    page: 1,
    flag_order: true,
    order_no: "",
    swip_arr: [],
    aunt_info: wx.getStorageSync('auntInfo').id

  },

  getAyOrder() {

    getApp().globalData.post({
      url: "/api/applet.oyc/ay_order.html"
    }, true).then(res => {

      this.setData({
        ay_order: res.data.data
      })
    })
  },
  getState(e) {
    this.setData({
      state: e.currentTarget.dataset.id,
      flag_order: true,
      page: 1,
      order_list: [],
      order_no: ""
    })
    this.getIssueOrderList()
  },
  //订单中心服务类型
  changetypescurr(e) { //顶部tao切换
    this.setData({
      typescurr: this.data.typescurr == e.currentTarget.dataset.id ? -1 : e.currentTarget.dataset.id,
      list2_index: e.currentTarget.dataset.index,

    })
  },
  search() {
    if (this.data.et) {
      if (!this.data.st) {
        wx.showToast({
          title: '开始时间不能为空',
          icon: 'none'
        })
        return
      }
    }
    if (this.data.st) {
      if (!this.data.et) {
        wx.showToast({
          title: '结束时间不能为空',
          icon: 'none'
        })
        return
      }
    }
    if (this.data.st > this.data.et) {
      wx.showToast({
        title: '开始时间不能高于结束时间',
        icon: 'none'
      })
      return
    }
    if (this.data._index > this.data.new_index) {
      wx.showToast({
        title: '最低值不能高于最高值',
        icon: 'none'
      })
      return
    }
    if (this.childEle) {
      this.childEle.ontop()
    } else {
      this.childEle = this.selectComponent('#childEle');
      this.childEle.ontop()
    }
    this.setData({
      tanflag: false
    })
  },
  onMyEvent(e) {
    if (e.detail.newtype == 1) {
      this.setData({
        type: e.detail.index
      })
    } else if (e.detail.newtype == 2) {
      let newindex = this.data.HomeList.findIndex(item => {
        return item.id == e.detail.index
      })
      console.log(newindex);
      if (newindex == 0) {
        this.setData({
          search: ''
        })
        return
      }
      let aa = '';
      aa = this.data.HomeList[newindex].title;
      this.setData({
        search: aa
      })
    } else if (e.detail.newtype == 3) {
      let searchself_list = this.data.searchself_list
      searchself_list.forEach((v, i) => {
        if (e.detail.index == 2) {
          this.setData({
            searchself: e.detail.index
          })
        }
      })
    }

  },
  input(e) {
    this.setData({
      keys: e.detail.value
    })
    if (this.childEle) {
      this.childEle.ontop()
    } else {
      this.childEle = this.selectComponent('#childEle');
      this.childEle.ontop()
    }
  },
  onLoad(options) {
    if (options.type) {
      this.setData({
        is_user: false,
        is_merchant: options.type == 'is_merchant' ? true : false,
        is_aunt: options.type == 'is_aunt' ? true : false,
        nav_curr: 3
      })
    }
    if (options.nav_curr) {
      this.setData({
        nav_curr: options.nav_curr
      })
    }
    this.getAyOrder()
    this.citylinkage()
    if (options.order) {
      this.setData({
        order_no: options.order
      })
    }
    this.setData({
      order_list: [],
      page: 1,
      flag_order: true
    })
    this.getIssueOrderList()
    this.get_swip_arr()
    wx.$TUIKit.on(wx.$TUIKitEvent.CONVERSATION_LIST_UPDATED, this.onConversationListUpdated);
    wx.getSystemInfo({
      success: (s) => {
        let windowWidth = s.windowWidth
        console.log(s.windowHeight - windowWidth / 750 * (96 + 104 + 64));
        this.setData({
          scroll_h: s.windowHeight - windowWidth / 750 * (96 + 104 + 64) - app
            .globalData.Tabheight,
        })
        return
        if (app.globalData.isIphoneX) {
          this.setData({
            //消息底部高度
            scroll_h: s.windowHeight - windowWidth / 750 * (64) - getApp()
              .globalData.Tabheight,
          })
        } else {
          this.setData({
            scroll_h: s.windowHeight - windowWidth / 750 * (64 + 132 + 104)
          })
        }
      },
    })

  },
  getIssueOrderList() {
    if (!this.data.flag_order) {
      return
    }
    getApp().globalData.post({
      url: "/api/applet.oyc/issue_order_list.html",
      data: {
        state: this.data.state,
        city: this.data.area,
        is_live_room: this.data.order_time,
        work_type: this.data.order_type,
        page: this.data.page,
        order_no: this.data.order_no
      }
    }, true).then(res => {
      wx.stopPullDownRefresh();
      let data = res.data.data
      if (data.length == 0) {
        this.setData({
          flag: false
        })
      } else {
        let arr = this.data.order_list.concat(data);
        this.setData({
          order_list: arr,
          flag_order: true,
          page: this.data.page + 1
        })
      }
    })
  },

  get_swip_arr() {
    getApp().globalData.post({
      data: {
        do: 'ReleaseWorkLlist',
        a: 'wxapp2',
        page: 1,
        pagesize: 10,
        uid: wx.getStorageSync('userInfo').id,
        st: 0,
        et: 60 * 60 * 24,
        money_st: '',
        money_et: '',
        type: 0,
        searchs: '',
        keys: '',
        searchself: ''
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      let arr = res.data.data;
      let swip = []
      arr.forEach((v, i) => {
        if (v.state != 8 && v.user_nickname) {
          swip.push('恭喜' + v.user_nickname + ",接单成功！")
        }
      })
      this.setData({
        swip_arr:swip
      })
    })
  },

  citylinkage() {
    return new Promise((reslove, reject) => {
      app.globalData.posts(`/api/app/user/sorting/ouyoung_api/gylarea?type=${this.data.typeCity}&province=${this.data.province}&city=${this.data.city}&region=${this.data.region}`).then(res => {
        if (res.data.code != 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          return
        }
        let list = [{
          CRI_NAME: '请选择',
          CRI_CODE: false
        }]
        list.push(...res.data.data)
        if (this.data.typeCity == 1) {
          this.setData({
            province_list: list,
            range_list: [list, this.data.city_list, this.data.region_list, this.data.street_list]
          })
          reslove(1)
        } else if (this.data.typeCity == 2) {
          this.setData({
            city_list: list,
            range_list: [this.data.province_list, list, this.data.region_list, this.data.street_list]
          })
          reslove(2)
        }
        if (this.data.typeCity == 3) {
          this.setData({
            region_list: list,
            range_list: [this.data.province_list, this.data.city_list, list, this.data.street_list]
          })
          reslove(3)
        }
        if (this.data.typeCity == 4) {
          this.setData({
            street_list: list,
            range_list: [this.data.province_list, this.data.city_list, this.data.region_list, list]
          })
          reslove(4)
          return
          if (res.data.data.length == 0) {
            this.setData({
              street_list: [],
              range_list: [this.data.province_list, this.data.city_list, this.data.region_list]
            })
          } else {
            this.setData({
              street_list: list,
              range_list: [this.data.province_list, this.data.city_list, this.data.region_list, list]
            })
            reslove(4)
          }
        }
      })
    })

  },
  change_picker(e) {
    let list = e.detail.value;
    let range_list = this.data.range_list;
    let flag = true
    list.forEach((v, i) => {
      if (range_list[i].length != 1 && v == 0) {
        flag = false
      }
    })
    if (!flag) {
      getApp().showToast('请正确选择所在地区')
      return
    }
    this.setData({
      value: list,
      area: range_list[0][list[0]].CRI_NAME + ',' + range_list[1][list[1]].CRI_NAME + ',' + range_list[2][list[2]].CRI_NAME + (range_list[3][list[3]].CRI_NAME == '请选择' ? '' : ',' + range_list[3][list[3]].CRI_NAME)
    })
    this.setData({

      flag_order: true,
      page: 1,
      order_list: [],
      order_no: ""
    })
    this.getIssueOrderList()
  },
  change_column(e) {
    if (e.detail.value == 0) {
      if (e.detail.column == 0) {
        this.setData({
          city_list: [],
          region_list: [],
          street_list: [],
        })
      } else if (e.detail.column == 1) {
        this.setData({
          region_list: [],
          street_list: [],
        })
      } else if (e.detail.column == 2) {
        this.setData({
          street_list: [],
        })
      } else if (e.detail.column == 3) {
        this.setData({

        })
      }
      this.setData({
        range_list: [this.data.province_list, this.data.city_list, this.data.region_list, this.data.street_list]
      })
      return
    }
    let value = this.data.value
    if (e.detail.column == 0) {
      this.setData({
        typeCity: 2,
        province: this.data.province_list[e.detail.value].CRI_CODE,
        city_list: [],
        region_list: [],
        street_list: [],
        value: [e.detail.value, 0, 0, 0]
      })
    } else if (e.detail.column == 1) {
      this.setData({
        typeCity: 3,
        city: this.data.city_list[e.detail.value].CRI_CODE,
        region_list: [],
        street_list: [],
        value: [value[0], e.detail.value, 0, 0]
      })
    } else if (e.detail.column == 2) {
      this.setData({
        typeCity: 4,
        street_list: [],
        region: this.data.region_list[e.detail.value].CRI_CODE,
        value: [value[0], value[1], e.detail.value, 0]
      })
    } else if (e.detail.column == 3) {
      this.setData({
        street: this.data.street_list[e.detail.value].CRI_CODE
      })
    }
    this.citylinkage()
  },
  bindPickerChange(e) {
    this.setData({
      order_type: this.data.work_type[e.detail.value].id,
      flag_order: true,
      page: 1,
      order_list: [],
      order_no: ""
    })

    this.getIssueOrderList()

  },
  bindPickerChange2(e) {
    this.setData({
      order_time: this.data.is_live_room[e.detail.value].id,
      flag_order: true,
      page: 1,
      order_list: [],
      order_no: ""
    })

    this.getIssueOrderList()
  },
  bindDateChangetwo(e) {
    console.log(e);
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        _index: this.data.array[e.detail.value]

      })
    } else {
      this.setData({
        new_index: this.data.new_array[e.detail.value]
      })
    }
  },

  Reset() {
    this.setData({
      _index: '',
      new_index: '',
      newdate: '',
      date: '',
      search: '',
      type: 0,
      st: '',
      et: '',
      searchself: '',
      oldmian: false,
      mianim: '',
    })
    if (this.data.nav_curr == 2) {
      this.Kuang = this.selectComponent('#Kuang');
      this.newKuang = this.selectComponent('#newKuang');
      this.newKuang3 = this.selectComponent('#newKuang3');
      this.Kuang.Reset()
      this.newKuang.Reset()
      this.newKuang3.Reset()
    }
  },
  onConversationListUpdated() {
    this.setData({
      list: app.globalData.conversationList,
      num: app.globalData.unreadCount
    })
  },
  bindDateChange(e) {
    console.log(e);
    let aa = '';

    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        date: e.detail.value,
        st: +new Date(e.detail.value)
      })
    } else {
      this.setData({
        newdate: e.detail.value,
        et: +new Date(e.detail.value)
      })
    }
  },
  onShow() {
    this.setData({
      list: app.globalData.conversationList,
      num: app.globalData.unreadCount
    })
    if (this.data.list.length == 0) {
      this.setData({
        article_page: 1,
        article_flag: true,
        article_list: []
      })
      this.newslist()
    }
  },
  onReachBottom() {
    this.getIssueOrderList()
  },
  newslist() {
    if (!this.data.article_flag) {
      return
    }
    this.setData({
      article_flag: false
    })
    getApp().globalData.posts('/api/app/device/sorting/oyaction/newslist', {
      page: this.data.article_page,
      size: 10
    }).then(res => {
      let data = res.data.data
      if (data.length == 0) {
        this.setData({
          article_flag: false
        })
      } else {
        let arr = this.data.article_list.concat(data)
        this.setData({
          article_list: arr,
          article_page: this.data.article_page + 1,
          article_flag: true,
        })
      }
    })
  },
  send_message(e) {
    this.setData({
      oldmian: false,
      show: true
    })
    getApp().globalData.post({
      data: {
        do: 'AuntMeeting',
        order_id: e.currentTarget.dataset.wid,
        aunt_id: wx.getStorageSync('auntInfo').id
      }
    }).then(res => {
      if (res.data.code == 200) {
        this.setData({
          oldmian: true,
          mianim: e.currentTarget.dataset.im
        })
        this.clost()
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  clost() {
    if (this.data.oldmian) {
      to_tim_chat(this.data.mianim)
    }
    return
    this.setData({
      show: false
    })
    if (this.data.nav_type == 0) {
      if (this.data.oldmian) {
        to_tim_chat(this.data.mianim)
      }
    } else {
      wx.navigateTo({
        url: './index/index?id=' + this.data.order_Id,
      })
    }

  },
  to_web(e) {
    let url = e.currentTarget.dataset.url
    url = encodeURIComponent(url)
    wx.navigateTo({
      url: '/pages/webview/web?url=' + url,
    })
  },

  change_nav_curr(e) {
    if (e.currentTarget.dataset.type != this.data.nav_curr) {
      this.setData({
        tanflag: false
      })
    }
    this.setData({
      nav_curr: e.currentTarget.dataset.type,
    })
    if (this.data.nav_curr == 1) {
      this.setData({
        scroll_h: getApp().globalData.screenHeight - getApp().globalData.windowWidth / 750 * (96 + 64 + 20) - app
          .globalData.Tabheight,
      })
    } else if (this.data.nav_curr == 2) {
      this.setData({
        scroll_h: getApp().globalData.screenHeight - getApp().globalData.windowWidth / 750 * (96 + 104 + 64) - app
          .globalData.Tabheight,
      })
    }
  },
  screen() {
    this.setData({
      tanflag: !this.data.tanflag
    })
  },
  scrolltolower() {
    if (this.data.nav_curr == 2) {
      if (this.childEle) {
        this.childEle.ReleaseWorkLlist()
      } else {
        this.childEle = this.selectComponent('#childEle');
        this.childEle.ReleaseWorkLlist()
      }
    } else if (this.data.nav_curr == 1) {
      this.newslist()
    }
  },
  get_swip(e) {
    this.setData({
      swip_arr: e.detail
    })
  },

  to_tim(e) {
    let type = e.currentTarget.dataset.type
    let conversationid = ""
    if (type == "C2C") {
      conversationid = e.currentTarget.dataset.conversationid
    } else if (type == "GROUP") {
      conversationid = e.currentTarget.dataset.group_id
    } else {
      conversationid = e.currentTarget.dataset.system
    }
    to_tim_chat(conversationid, type)
  },
  to_aunt(e) {
    let im = e.currentTarget.dataset.im
    getApp().ishouse({
      im
    }).then(data => {
      if (data.data.ishouse == 1) {
        wx.navigateTo({
          url: '/pages/aunt/index?type=1&uid=' + data.data.house.id
        })
      }
    })
  },

  onShareAppMessage(e) {
    let from = e.from
    let url = ''
    // console.log(e,'45');
    if (from == "button") {
      url = encodeURIComponent('/pages/news/news?order=' + e.target.dataset.order + '&nav_curr=3')
      return {
        path: '/pages/index/blank?to_id=' + wx.getStorageSync('userInfo').id + '&tourl=' + url,
        title: '超有幸福家政',
      }

    } else {
      url = encodeURIComponent('/pages/index/index')
      return {
        path: '/pages/index/blank?to_id=' + wx.getStorageSync('userInfo').id + '&tourl=' + url,
        title: '超有幸福家政',
      }
    }

  },

})